SET(MITK_USE_BLUEBERRY 1)
SET(MITK_USE_EXT 1)
FIND_PACKAGE(MITK)

# TODO: this can be removed after resolving factory issues, it's only necessary for the registration call in main()
MITK_USE_MODULE(MitkExt)

INCLUDE_DIRECTORIES(
  ${BLUEBERRY_PLUGIN_SOURCE_DIRS} 
  ${org.blueberry.osgi_BIN_DIR} # needed for generated berryConfig.h 
  ${Poco_INCLUDE_DIRS}
  ${ALL_INCLUDE_DIRECTORIES}
)
LINK_DIRECTORIES(${Poco_LIBRARY_DIRS})
  
IF(MITK_SHOW_CONSOLE_WINDOW)
  ADD_EXECUTABLE(FitMe MACOSX_BUNDLE FitMe.cpp)
ELSE(MITK_SHOW_CONSOLE_WINDOW)
  ADD_EXECUTABLE(FitMe MACOSX_BUNDLE WIN32 FitMe.cpp)
ENDIF(MITK_SHOW_CONSOLE_WINDOW)

IF (WIN32)
  FIND_PACKAGE(Qt4)
  TARGET_LINK_LIBRARIES(FitMe ${QT_QTCORE_LIBRARY} ${QT_QTMAIN_LIBRARY})
ENDIF(WIN32)

TARGET_LINK_LIBRARIES(FitMe 
                      optimized PocoFoundation      debug PocoFoundationd 
                      optimized PocoUtil            debug PocoUtild 
                      optimized org_blueberry_osgi debug org_blueberry_osgi${BLUEBERRY_DEBUG_POSTFIX}
                      ${ALL_LIBRARIES})

SET_TARGET_PROPERTIES(FitMe PROPERTIES INSTALL_RPATH "${CMAKE_INSTALL_RPATH};${CMAKE_INSTALL_RPATH}/BlueBerry/org.blueberry.osgi/bin")
					  
SET(FITME_REQUIRED_BUNDLES
org.mitk.gui.qt.datamanager
org.mitk.gui.qt.imagenavigator
)
FOREACH(FITME_REQUIRED_BUNDLE ${FITME_REQUIRED_BUNDLES})
  SET(MITK_BUILD_${FITME_REQUIRED_BUNDLE} "ON" CACHE BOOL "Build ${FITME_REQUIRED_BUNDLE} Plugin" FORCE)
  STRING(REPLACE "." "_" FITME_REQUIRED_BUNDLE_PROJECT ${FITME_REQUIRED_BUNDLE})
  SET(MITK_MODULES_ENABLED_PLUGINS ${MITK_MODULES_ENABLED_PLUGINS} ${FITME_REQUIRED_BUNDLE_PROJECT})
ENDFOREACH()

#MESSAGE(STATUS ${MITK_MODULES_ENABLED_PLUGINS})
					  
SET(_plugin_deps ${BLUEBERRY_ENABLED_PLUGINS} ${MITK_CORE_ENABLED_PLUGINS} ${MITK_MODULES_ENABLED_PLUGINS})

IF(_plugin_deps)
  # Make sure all enabled plug-ins are up to date
  ADD_DEPENDENCIES(FitMe ${_plugin_deps})
  # TARGET_LINK_LIBRARIES(FitMe ${_plugin_deps})
ENDIF()

SET(BLUEBERRY_PLUGIN_CACHE_DIR "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/plugin_cache")

CONFIGURE_FILE(FitMe.ini
               ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/FitMe.ini)

IF(WIN32)
  SET(FITME_BUILD_TYPE debug)
  CONFIGURE_FILE(startExtApp.bat.in
               ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/startExtApp_${FITME_BUILD_TYPE}.bat @ONLY)
  SET(FITME_BUILD_TYPE release)
  CONFIGURE_FILE(startExtApp.bat.in
               ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/startExtApp_${FITME_BUILD_TYPE}.bat @ONLY)
ENDIF(WIN32)

MITK_INSTALL_TARGETS(EXECUTABLES FitMe GLOB_PLUGINS )

IF(UNIX AND MITK_INSTALL_RPATH_RELATIVE AND NOT APPLE)
  INSTALL(PROGRAMS "${MITK_SOURCE_DIR}/CMake/RunInstalledApp.sh" DESTINATION "." RENAME FitMe.sh)
ENDIF()
SET(MITK_CPACK_PACKAGE_EXECUTABLES ${MITK_CPACK_PACKAGE_EXECUTABLES} "FitMe;MITK - FitMe Application" CACHE INTERNAL "Collecting windows shortcuts to executables")
